#include "TIM.h"
#include "stm32f10x.h"


void init_tim(void){
    
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
    
    
    TIM_InternalClockConfig(TIM4);
    
    TIM_TimeBaseInit(TIM4,&(TIM_TimeBaseInitTypeDef){
        // 最终中断触发频率是 100Hz
        // 即 10ms 触发一次中断
        .TIM_Prescaler=7200-1,
        .TIM_CounterMode = TIM_CounterMode_Up,
        .TIM_Period = 100,
        .TIM_ClockDivision=TIM_CKD_DIV1
    
    });

 
    
    TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
    
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    
    NVIC_Init(&(NVIC_InitTypeDef){
    
        .NVIC_IRQChannel = TIM4_IRQn,
        .NVIC_IRQChannelPreemptionPriority=2,
        .NVIC_IRQChannelSubPriority=2,
        .NVIC_IRQChannelCmd= ENABLE
    
    });

    TIM_Cmd(TIM4,ENABLE);
}

